package pluginUtils;

import java.util.ArrayList;

//import java.util.Random;


public class PackCircle {
	
	//private static Random generator=new Random(); 
	
	private static int order=0; 
	private static ArrayList<PackCircle> allCircles=new ArrayList<PackCircle>();
	
	private int myOrder; 
	private double radius=0.2; //generator.nextDouble();; 
	private boolean internal; 
	private int nbNeighbours; 
	private PackCircle neighbours[];
	private double x,y;
	private boolean coordset=false; 
	private int couleur; 
	
	public PackCircle(int nb,boolean intern,double r,int c){
	this(nb,intern,c); 
	this.radius=r;  
	}
	
	
	
	public PackCircle(int nb,boolean intern,int c){
		allCircles.add(order,this);
		this.myOrder=order++; 	
		this.nbNeighbours=nb; 
		this.internal=intern;
		this.neighbours=new PackCircle[nb]; 
		this.couleur=c; 
	}
	
	public static PackCircle getCircleAt(int index){
		return allCircles.get(index); 
	}

	public String toString(){
		return this.radius+" "+x+" "+y+"( numero "+this.getMyOrder()+")"; 
	}
	public boolean isInternal(){return this.internal; }
	
	public PackCircle getNeighbourAt(int i){
			return neighbours[i]; 
	}
	
	public void setNeighbourAt(int i,PackCircle c){
			this.neighbours[i]=c; 
	}
	
	/**
	 * @return the radius
	 */
	public double getRadius() {
		return radius;
	}

	/**
	 * @param radius the radius to set
	 */
	public void setRadius(double radius) {
		this.radius = radius;
	}
	
	/** Compute the sum of angles with neighbours */
	
	public double computeTheta(){
		double sum=0; 
		double x=this.radius; 
		for(int i=0;i<this.nbNeighbours;i++){
			double y=this.neighbours[i].radius; 
			double z=this.neighbours[(i+1)%this.nbNeighbours].radius;
			//System.out.println("rayons "+x+" "+y+" "+z); 
			double alpha=(x+y)*(x+y)+(x+z)*(x+z)-(y+z)*(y+z); 
			alpha=alpha/(2*(x+y)*(x+z)); 
			sum=sum+Math.acos(alpha); 
		}
		return sum; 
	}

	/**
	 * @return the nbNeighbours
	 */
	public int getNbNeighbours() {
		return nbNeighbours;
	}

	/**
	 * @return the x
	 */
	public double getX() {
		return x;
	}

	/**
	 * @param x the x to set
	 */
	public void setX(double x) {
		this.x = x;
		this.coordset=true; 
	}

	/**
	 * @return the y
	 */
	public double getY() {
		return y;
	}

	/**
	 * @param y the y to set
	 */
	public void setY(double y) {
		this.y = y;
		this.coordset=true; 
	}

	/**
	 * @return the coordset
	 */
	public boolean isCoordset() {
		return coordset;
	}

	/**
	 * @return the myOrder
	 */
	public int getMyOrder() {
		return myOrder;
	}

	public static double dist(PackCircle u, PackCircle e) {
		double sum=(u.getX()-e.getX())*(u.getX()-e.getX()); 
		sum+=(u.getY()-e.getY())*(u.getY()-e.getY()); 
		return Math.sqrt(sum);
	}

	/**
	 * @return the couleur
	 */
	public int getCouleur() {
		return couleur;
	}

	/**
	 * @param couleur the couleur to set
	 */
	public void setCouleur(int couleur) {
		this.couleur = couleur;
	}
	
	public static int size(){return allCircles.size();}

}
